using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using UnityEngine;

public class Turret : MonoBehaviour
{
	[SerializeField]
	private BaseWeapon weapon;

	[SerializeField]
	private Collider ignore;

	[SerializeField]
	private Transform point;

	[SerializeField]
	private float timeShoot = 4f;

	[SerializeField]
	private float spread = 4f;

	[SerializeField]
	private List<Vector2> rotations;

	private Vector2 startRotation;

	private float timeFromShoot;

	private int indexRotations;

	private Coroutine rotationCoroutine;

	private void Awake()
	{
		weapon.Init(1, point, null, ignore, true);
		weapon.isMobWeapon = true;
		weapon.chargerAmmo = 10;
		startRotation = point.transform.localRotation.eulerAngles;
	}

	private void Update()
	{
		if (PhotonNetwork.isMasterClient)
		{
			float num = Random.Range(0f, 0.3f);
			if ((timeFromShoot += Time.deltaTime) > timeShoot + num)
			{
				weapon.strength = 1f;
				weapon.chargerAmmo = 10;
				weapon.spreadWeapon = spread;
				weapon.Shoot(weapon.transform.position + weapon.transform.forward * 50f);
				timeFromShoot = 0f;
				Rotate();
			}
		}
	}

	private void Rotate()
	{
		if (rotations.Count > 0)
		{
			if (rotationCoroutine != null)
			{
				StopCoroutine(rotationCoroutine);
				rotationCoroutine = null;
			}
			Vector2 rotation;
			if (indexRotations >= rotations.Count)
			{
				indexRotations = 0;
				rotation = startRotation;
			}
			else
			{
				rotation = rotations[indexRotations++];
			}
			rotationCoroutine = StartCoroutine(Rotation(rotation, timeShoot));
		}
	}

	private IEnumerator Rotation(Vector2 rotation, float time)
	{
		yield return new WaitForSeconds(0.2f);
		Quaternion newRotation2 = Quaternion.Euler(new Vector3(point.transform.localRotation.eulerAngles.x, rotation.y, 0f));
		float curTime = 0f;
		HOTween.To(point.transform, time / 2.3f, new TweenParms().Prop("localRotation", newRotation2));
		while (curTime < time / 2.2f)
		{
			curTime += Time.deltaTime;
			yield return null;
		}
		point.transform.localRotation = newRotation2;
		curTime = 0f;
		yield return new WaitForSeconds(0.2f);
		newRotation2 = Quaternion.Euler(new Vector3(rotation.x, point.transform.localRotation.eulerAngles.y, 0f));
		HOTween.To(point.transform, time / 2.3f, new TweenParms().Prop("localRotation", newRotation2));
		while (curTime < time / 2.2f)
		{
			curTime += Time.deltaTime;
			yield return null;
		}
		point.transform.localRotation = newRotation2;
	}
}
